
// g++ direction_arrows.cc -lX11 -lXtst -o direction_arrows

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 */

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


static void  send_key(Display * disp, KeySym keysym)
{
    XTestGrabControl(disp, 1);
    
    XTestFakeKeyEvent(disp, XKeysymToKeycode(disp, keysym), 1, 0);
    XTestFakeKeyEvent(disp, XKeysymToKeycode(disp, keysym), 0, 0);
    
    XSync(disp, 0);
    XTestGrabControl(disp, 0);
}

int  main(int argc, char *argv[])
{
    Display *disp = XOpenDisplay (NULL);

    for(int i = 0; i < 20; i++)
    {
        sleep(1);
        
        switch(i%4)
        {
            case 0:
                send_key(disp, XK_Right);
                printf("pressing right\n");
                break;
            case 1:
                send_key(disp, XK_Left);
                printf("pressing left\n");
                break;
            case 2:
                send_key(disp, XK_Down);
                printf("pressing down\n");
                break;
            case 3:
                send_key(disp, XK_Up);
                printf("pressing up\n");
                break;
            default:
                printf("what's wrong with me!\n");
        }
    }
    
    printf("\n");
    XCloseDisplay(disp);
    return 0;
}

